/**
 * @ author: 汪孟苹
 */
define(["jquery", "template", "echarts", "fu/app/comm/comm",
	"scprit/followup/app/question/imgCollection.servers.js",
	"scprit/followup/app/question/question.servers.js",
	"scprit/followup/template/question/question.tpl.js"], function ($, template, echarts, $comm, $imgCollectionServers, $questionServers, tpl) {
		var questionUi = {}
		/**
		 * 根据手机号码查询患者
		 */
		var drawPatientsByPhone = function (data) {
			return template("tpl_question_patientsByPhone", data)
		}
		questionUi.patientsByPhone = function (options) {
			var data = {
				rows: $questionServers.handlePatientsByPhone(options.list)
			}
			options.dom.html(tpl.patientsByPhone())
			var h = drawPatientsByPhone(data)
			options.dom.html(h)
		}
		/**
			 * 绘制曲线图
			 */
		questionUi.curve = function (data, className, label, gender, interval) {
			var divId = "chart-" + $comm.random()
			$comm.createDiv({
				dom: $("." + className + " .modal-body"),
				id: divId,
			})
			if (data.curveType == "01") { // growth曲线
				$("#" + divId).css({ "height": 470, "width": 570 })
				$("#" + divId).parents(".modal-content").css({ "width": 600 })
			} else if (data.curveType == "02") {  // fenton曲线
				$("#" + divId).css({ "height": 855, "width": 570 })
				$("#" + divId).parents(".modal-content").css({ "width": 600 })
			}
			var myChart = echarts.init(document.getElementById(divId))
			var option = $questionServers.curveOption(data, interval, label, gender)
			myChart.setOption(option)
		}

		/**
		 * 评估
		 */
		var drawAssessFooterUi = function (data) {
			return template("tpl_question_assessFooter", data)
		}
		questionUi.assessFooterInit = function (options) {
			var data = { "flag": false }
			data = $.extend(data, { "flag": options.flag })
			options.dom.html(tpl.assessFooter())
			var html = drawAssessFooterUi(data)
			options.dom.html(html)
		}
		// 1、高危儿孕产妇评估
		var drawHighRiskUi = function (data) {
			return template("tpl_question_highRisk", data)
		}
		questionUi.highRiskInit = function (options) {
			options.dom.html(tpl.highrisk())
			var html = drawHighRiskUi(options)
			options.dom.html(html)
		}
		// 2、Alberta婴幼儿运动量评估
		var drawAimsUi = function (data) {
			return template("tpl_question_aims", data)
		}
		questionUi.aimsInit = function (options) {
			options.dom.html(tpl.aims())
			var html = drawAimsUi(options)
			options.dom.html(html)
		}

		/**
		 * 融合基因
		 */
		var drawProjectItemUi = function (data) {
			return template("tpl_question_projectItem", data)
		}
		questionUi.projectItemInit = function (options) {
			options.dom.html(tpl.projectItem())
			var html = drawProjectItemUi(options)
			options.dom.html(html)
		}
		var drawProjectItemManagerUi = function (data) {
			return template("tpl_question_projectItemManager", data)
		}
		questionUi.projectItemManagerInit = function (options) {
			options.dom.html(tpl.projectItemManager())
			var html = drawProjectItemManagerUi(options)
			options.dom.html(html)
		}

		/**
		 * 图像处理
		 */
		// 图像处理（添加图片）
		var drawAddImageProcessing = function (data) {
			return template("tpl_question_addImageProcessing", data)
		}
		questionUi.addImageProcessingInit = function (options) {
			options.dom.html(tpl.addImageProcessing())
			var html = drawAddImageProcessing(options.data)
			options.dom.html(html)
		}
		// 图像处理（编辑图片）
		var drawEditImageProcessing = function (data) {
			return template("tpl_question_editImageProcessing", data)
		}
		questionUi.editImageProcessingInit = function (options) {
			options.dom.html(tpl.editImageProcessing())
			var html = drawEditImageProcessing(options.data)
			options.dom.html(html)
		}
		//添加图片：图片展示部分：
		var drawAddImg_imageBox = function (data) {
			return template("tpl_question_addImg_imageBox", data)
		}
		questionUi.addImg_imageBoxInit = function (options) {
			options.dom.html(tpl.addImg_imageBox())
			var html = drawAddImg_imageBox(options.data)
			options.dom.html(html)
		}
		/**
		 * 设置多下拉、多输入的宽度
		 */
		var setMultipleWidth = function () {
			/* 多下拉、多输入 */
			$(".qaCard .multiple select,.qaCard .multiple p.form-control-static,.qaCard .multiple input").each(function (i) {		// 孕周 多下拉框
				$(this).css({
					width: Math.floor($(this).parent().width()) - Math.ceil($(this).parent().find("span").width()) - 1 + 'px'
				})
			})
		}

		//标记模板：
		var drawQuestionUi_mark = function (data) {
			return template("tpl_question_total", data)
		}
		questionUi.init_mark = function (options) {
			var data = $questionServers.getQuestionData(options)
			var formId = "questionForm_" + $comm.random()
			var markId = options.markId ? options.markId : ""
			$("#mark-describe-tit ul li").removeClass('active')
			var li = '<li class="active" formId = "' + formId + '" num = "' + options.num + '" markType = "' + options.markType + '" templVersion = "' + options.templVersion + '" templId = "' + options.templId + '" markId = "' + markId + '"  >' + options.markTitle + '</li>'
			$("#mark-describe-tit ul").append(li)

			$.extend(data, { "formId": formId })
			$("#" + options.id).append(tpl.init())
			var html = drawQuestionUi_mark(data)
			$("#" + options.id).append(html)

			$("#" + options.id + " form").each(function () {
				if ($(this).attr("id") == formId) {
					$("#" + formId).removeClass("hide")
				} else {
					$(this).addClass("hide")
				}
			})

			$("#" + options.id).find("script").remove()
			setMultipleWidth()
			$(window).resize(setMultipleWidth)
		}


		/**
		 * 问卷
		 */
		var drawQuestionUi = function (data) {
			return template("tpl_question_total", data)
		}
		questionUi.init = function (options) {
			var data = $questionServers.getQuestionData(options)
			var formId = "questionForm_" + $comm.random()
			$.extend(data, { "formId": formId })
			$("#" + options.id).html(tpl.init())
			// console.log('data000', data)
			var html = drawQuestionUi(data)
			$("#" + options.id).html(html)
			// 动态请求后，将返回的放进去
			$questionServers.setInitData(options.id, options.initParams)
			setMultipleWidth()
			$(window).resize(setMultipleWidth)

			//图像编辑的图片渲染：
			if (options.values) {
				getImageSignUrl(options.values)
			}
		}
		//影像编辑：渲染图片时，提交记录中的图片渲染：
		var getImageSignUrl = function (values) {
			for (var item in values) {
				if (values[item]) {
					var length = values[item].length
					var first = values[item].substring(0, 1)
					var last = values[item].substring(length - 1)
					var flag = (first == "{" && last == "}") ? true : false
					if (values[item] && flag) {
						if (typeof JSON.parse(values[item]) == 'object' && !(JSON.parse(values[item]) instanceof Array)) {
							var params = {}
							var imgEdit = JSON.parse(values[item])
							var imgList = imgEdit.imageEdit
							if (imgList && imgList.length > 0) {
								var fileList = []
								for (var i = 0; i < imgList.length; i++) {
									fileList.push({
										"imgId": imgList[i].imgId,
										"imgUrl": imgList[i].imgUrl
									})
								}
								params.name = item
								params.imgList = imgList
								$imgCollectionServers.getImgSignatureUrl({ "fileList": fileList }, getImgSignatureUrlCallback, params)
							}
						}
					}
				}
			}
		}
		var getImgSignatureUrlCallback = function (data, params) {
			if (data.success) {
				var fileList = data.data.fileList
				var imgList = params.imgList
				var h = ""
				for (var i = 0; i < fileList.length; i++) {
					for (var k = 0; k < imgList.length; k++) {
						if (imgList[k].imgId == fileList[i].imgId) {
							fileList[i] = $.extend(fileList[i], imgList[k])
						}
					}
					var markIdList = fileList[i].markIdList ? JSON.stringify(fileList[i].markIdList) : ""
					var markHtml = markIdList ? ("markIdList=" + markIdList) : ''
					h += '<div class="picture" ' + markHtml + ' imgOldUrl = "' + fileList[i].imgUrl + '" style="background:url(' + fileList[i].imgNewUrl + ') no-repeat center;background-size:cover" ImgId = "' + fileList[i].imgId + '">' +
						'		<div class="picture-box">' +
						'			<span class="imageProcessing-del" ><i class="fa fa-trash-o"></i>删除</span>' +
						'			<span class="imageProcessing-edit" ><i class="fa fa-pencil-square-o"></i>编辑</span>' +
						'		</div>' +
						'	</div>'
				}
				$('div[name="' + params.name + '"]').prev(".qaCard-imageBox").append(h)
			}
		}
		return questionUi
	})
